/*
* brief: term_cmd
* author: xu yue QQ:306863186.
* modify: 
* $Header: file:///E:/SRC_STORAGE/term_cmd/term_cmd.h 37 2013-02-02 08:58:37Z administrator $
* $Id: term_cmd.h 8691 2013-10-11 11:32:20Z yuexu $
* $URL: http://192.168.0.254/svn/HB_DVR/2.PM/2.5%20Software/branches/5800/inc/cmdline/term_cmd.h $
* $Author: yuexu $
* $Revision: 8691 $
* $Date: 2013-10-11 19:32:20 +0800 (五, 11 10月 2013) $
*/
#ifndef TERM_CMD_H
#define TERM_CMD_H

#define TERM_CMD_QUIT_ANNOUNCE (-0xBEEFDEAD)/*default quit cmd return it.*/

typedef int rl_icpfunc_fnp (char *);
/* Generator function for command completion.  
   STATE lets us know whether to start from scratch; 
   without any state (i.e. STATE == 0), 
   then we start at the top of the list.
*/
typedef char* rl_compentry_fnp (const char * /*text*/, int /*state*/);

/* A structure which contains information on the commands this program
   can understand. */
typedef struct
{
    char *name;         /* User printable name of the function. */
    rl_icpfunc_fnp *func; /* Function to call to do the job. */
    void* arg;          /* arguments addr for function directly.*/
    char *doc;          /* Documentation for this function.  */
} COMMAND_S;


typedef struct
{
    char* app_prompt;
    COMMAND_S* cmd_list;
}RL_CMD_PARAMS_S;
/*
    RL_CMD_PARAMS_S * rl_cmd_params : command list
    rl_icpfunc_t defualt : defualt proc function for the cmd that not found in the list.
    rl_compentry_fnp compelete : function for auto complete, to return match text addr;
*/
int term_cmd_wait(RL_CMD_PARAMS_S * rl_cmd_params, rl_icpfunc_fnp defualt, rl_compentry_fnp compelete);

/*
    Get CMD's option, such as:
            prn         -o term 
         CMD^  1st option^  ^1st option's parameter
    function param:
        line : the function 'rl_icpfunc_fnp' 's param, contains option(s).
        popt : one of the option.
        popt_param : current' option's parameter.
        opt_list : the valid option list.
    return value:
        address @ the next option's beginning.
*/
char* term_cmd_getopt(char* line, char *popt, char* popt_param, const char opt_list[]);

#define TERM_CMD_ARGS_PARSE_BGN(args, opt_list) do {\
    char *pcmd_arg = args; \
    char opt_cmd = 0; \
    char opt_param_cmd[128] = {0}; \
    while(NULL != (pcmd_arg = term_cmd_getopt(pcmd_arg, &opt_cmd, opt_param_cmd, opt_list))) \
    {
#define TERM_CMD_GET_OPTION() opt_cmd
#define TERM_CMD_GET_OPT_PARAM() opt_param_cmd
#define TERM_CMD_ARGS_PARSE_END() memset(opt_param_cmd, 0, sizeof(opt_param_cmd));}}while(0)
#endif
